from unittest import TestCase

import pymysql
from ddt import ddt, data, unpack
from selenium import webdriver

import Data
import Operation


@ddt
class Test_ShenPi(TestCase):
    @data(*Data.borrow.shenpi)
    @unpack
    def test_shenpi(self, name, pwd, expect):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=10000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()

        borrow = Operation.Borrow()
        borrow.shenpi()
        result = borrow.shenpi_result()
        self.assertEqual(20000.00, result)


@ddt
class Test_Borrow(TestCase):
    @data(*Data.borrow.zhengchang)
    @unpack
    def test_zhengchang(self, name, pwd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        borrow = Operation.Borrow()
        a = borrow.zhengchang(name, pwd, driver)
        newa = borrow.zhengchang_result(driver)
        if a + 1 != newa:
            driver.save_screenshot('lend1.jpg')
        self.assertEqual(a + 1, newa)

    @data(*Data.borrow.nocard)
    @unpack
    def test_nocard(self, name, pwd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        borrow = Operation.Borrow()
        borrow.nocard(name, pwd, driver)
        result = borrow.nocard_result(driver)
        if result != expect:
            driver.save_screenshot('lend3.jpg')
        self.assertEqual(result, expect)

    @data(*Data.borrow.big)
    @unpack
    def test_big(self, name, pwd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        borrow = Operation.Borrow()
        borrow.big(name, pwd, driver)
        result = borrow.big_result(driver)
        if result != expect:
            driver.save_screenshot('lend4.jpg')
        self.assertEqual(result, expect)

    @data(*Data.borrow.small)
    @unpack
    def test_small(self, name, pwd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        borrow = Operation.Borrow()
        borrow.small(name, pwd, driver)
        result = borrow.small_result(driver)
        if result != expect:
            driver.save_screenshot('lend5.jpg')
        self.assertEqual(result, expect)

    @data(*Data.borrow.maxtime)
    @unpack
    def test_maxtime(self, name, pwd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        borrow = Operation.Borrow()
        borrow.maxtime(name, pwd, driver)
        result = borrow.maxtime_result(driver)
        if result != expect:
            driver.save_screenshot('lend6.jpg')
        self.assertEqual(result, expect)

    @data(*Data.borrow.time)
    @unpack
    def test_time(self, name, pwd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        borrow = Operation.Borrow()
        borrow.time(name, pwd, driver)
        result = borrow.time_result(driver)
        if result != expect:
            driver.save_screenshot('lend6.jpg')
        self.assertEqual(result, expect)
